<%@ Assembly Name="Microsoft.SharePoint, Version=12.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c" %>

<%@ Page Language="C#" MasterPageFile="~/_layouts/application.master" Inherits="System.Web.UI.Page" %>

<%@ Import Namespace="Microsoft.SharePoint" %>
<%@ Import Namespace="Microsoft.SharePoint.Administration" %>

<script runat="server">
    private const string webAppPropertyKey = "Custom404Path";

    protected override void OnPreRender(EventArgs e)
    {
        SPSecurity.RunWithElevatedPrivileges(delegate()
        {
            using (SPSite thisSite = new SPSite(SPContext.Current.Site.Url))
            {
                string custom404Path = "";
                string pageNotFound = Request.QueryString["oldUrl"];
                pageNotFound = pageNotFound.ToLower();
                SPWebApplication webApp = thisSite.WebApplication;

                
                    #region Try Language 404
                    try
                    {
                        Uri uri = new Uri(pageNotFound);
                        using (SPSite site = new SPSite(pageNotFound))
                        {
                            using (SPWeb web = site.OpenWeb(uri.AbsolutePath, false))
                            {
                                #region Recurse to the first level SubWeb
                                Guid cweb = web.ID;
                                while (!cweb.Equals(Guid.Empty))
                                {
                                    using (SPWeb web2 = site.OpenWeb(cweb))
                                    {
                                        cweb = web2.ParentWebId;
                                        if (site.RootWeb.ID.Equals(cweb))
                                        {

                                            custom404Path = string.Format("{0}/Pages/404.aspx", web2.ServerRelativeUrl);
                                        }
                                    }
                                }
                                #endregion
                            }                            
                         }
                    }
                    catch (Exception ex)
                    {
                        SPSecurity.RunWithElevatedPrivileges(delegate
                        {
                            System.Diagnostics.EventLog.WriteEntry(this.GetType().FullName, string.Format("Unable to resolve '{0}'\r\n{1}", pageNotFound, ex.ToString()));
                        });
                    }
                    #endregion


                if (string.IsNullOrEmpty(custom404Path) && webApp.Properties.ContainsKey(webAppPropertyKey))
                {
                    custom404Path = (string)webApp.Properties[webAppPropertyKey];
                }

                if (!string.IsNullOrEmpty(custom404Path) && !custom404Path.ToLower().Contains("custom404handler.aspx") && !pageNotFound.ToLower().Contains(custom404Path.ToLower()) && !pageNotFound.ToLower().Contains("404.aspx") )
                {
                    Response.StatusCode = 302;
                    Response.Redirect(custom404Path + "?oldUrl=" + Server.UrlEncode(pageNotFound));
                }
                    
                
            }
        });
    }

</script>

<asp:Content ID="Main" runat="server" ContentPlaceHolderID="PlaceHolderMain">
    <p>
        The page you are trying to reach cannot be found.
    </p>
    <asp:Label ID="lblMessage" runat="server" />
</asp:Content>
<asp:Content ID="PageTitle" runat="server" ContentPlaceHolderID="PlaceHolderPageTitle">
    404 - Page Not Found
</asp:Content>
<asp:Content ID="PageTitleInTitleArea" runat="server" ContentPlaceHolderID="PlaceHolderPageTitleInTitleArea">
    404 - Page Not Found
</asp:Content>
